package route

import (
	"github.com/gin-gonic/gin"
	"go-shop-admin/api/apiv1"
	"go-shop-admin/internal/route/middleware"
	"net/http"
)

/*
	路由.go
	author:zydh
*/

// 路由
func route(r *gin.Engine) {

	r.GET("/", apiv1.Index)

	v1 := r.Group("api/v1")
	{
		//不使用token
		v1.GET("/tests", apiv1.Tests)
		//获取验证码
		//验证码
		v1.GET("/captcha", apiv1.GetCaptcha) //获取验证码
		//v1.POST("/captcha", apiv1.CheckCaptcha) //验证验证码

		//登录
		v1.POST("/login", apiv1.Login)

		//要用token/登录保护
		useToken := v1.Group("", middleware.JWTAuthMiddleware())
		{

			//管理员
			auth := useToken.Group("/auth")
			{
				//lists.GET("", api.ListsIndex)
				auth.GET("/:id", apiv1.AuthShow)       //查询详情
				auth.POST("", apiv1.AuthAdd)           //新增
				auth.PUT("/:id", apiv1.AuthUpdate)     //修改
				auth.DELETE("/:ids", apiv1.AuthDelete) //单条/批量删除
			}

			//角色管理
			authRole := useToken.Group("/role")
			{
				//lists.GET("", api.ListsIndex)
				authRole.GET("/:id", apiv1.AuthRoleShow)       //查询详情
				authRole.POST("", apiv1.AuthRoleAdd)           //新增
				authRole.PUT("/:id", apiv1.AuthRoleUpdate)     //修改
				authRole.DELETE("/:ids", apiv1.AuthRoleDelete) //单条/批量删除
			}
		}

	}

}

// 加载
func load() {
	r := gin.Default()

	//全局注册中间件  跨域中间件
	r.Use(middleware.CrossDomain())

	//加载静态文件的路径
	r.StaticFS("/static", http.Dir("./static"))

	//路由
	route(r)

	//启动http服务，默认在0.0.0.0:8080启动服务
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

// LoadRoute 路由加载
func LoadRoute() {
	load()
}
